<script>
import {getUrl} from "@/util/aihuman/getUrl";

export default {
  name: "PreviewDialog",
  props: {
    type: String
  },
  data() {
    return {
      dialogVisible: false,
      url: ''
    }
  },
  methods: {
    getUrl,
    openDialog(url) {
      this.dialogVisible = true
      this.url = this.getUrl() + url
    },
    closeDialog() {
      this.dialogVisible = false
      if (this.type === 'audio') {
        this.$refs.audioRef.pause()
        this.$refs.audioRef.currentTime = 0
      }
      if (this.type === 'video') {
        this.$refs.videoRef.pause()
        this.$refs.videoRef.currentTime = 0
      }
    }
  }
}
</script>

<template>
  <el-dialog
    title="预览"
    :visible.sync="dialogVisible"
    append-to-body
    top="2vh"
    width="1000px">
    <div v-if="type === 'image'" class="preview-box">
      <img :src="url" alt="">
    </div>

    <div v-if="type === 'video'" class="preview-box">
      <video ref="videoRef" controls :src="url"></video>
    </div>

    <div v-if="type === 'audio'" class="preview-box">
      <audio ref="audioRef" controls :src="url"></audio>
    </div>
    <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="closeDialog">关 闭</el-button>
  </span>
  </el-dialog>
</template>

<style scoped lang="scss">
.preview-box {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  img, video {
    height: 100%;
    width: 100%;
    object-fit: contain;
  }
}
</style>
